﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Controls.Primitives;

namespace EIA.SLClient.Utility.Behaviors
{
    public class Background7StateBehavior : Behavior<ToggleButton>
    {
        private Brush brush = null;
        protected override void OnAttached()
        {
            base.OnAttached();
            if (AssociatedObject != null)
            {
                brush = this.AssociatedObject.Background;
                RepresendBackgroundAsync();
                this.AssociatedObject.MouseEnter += OnMouseEnter;
                this.AssociatedObject.MouseLeave += OnMouseLeave;
                this.AssociatedObject.MouseLeftButtonDown += OnMouseLeftButtonDown;
                this.AssociatedObject.MouseLeftButtonUp += OnMouseLeftButtonUp;
                this.AssociatedObject.Checked += OnIsCheckChanged;
                this.AssociatedObject.Unchecked += OnIsCheckChanged;
                this.AssociatedObject.IsEnabledChanged += OnIsEnableChanged;
            }
        }
        protected override void OnDetaching()
        {
            base.OnDetaching();
            if (AssociatedObject != null)
            {
                this.AssociatedObject.MouseEnter -= OnMouseEnter;
                this.AssociatedObject.MouseLeave -= OnMouseLeave;
                this.AssociatedObject.MouseLeftButtonDown -= OnMouseLeftButtonDown;
                this.AssociatedObject.MouseLeftButtonUp -= OnMouseLeftButtonUp;
                this.AssociatedObject.Background = brush;
                this.AssociatedObject.Checked -= OnIsCheckChanged;
                this.AssociatedObject.Unchecked -= OnIsCheckChanged;
                this.AssociatedObject.IsEnabledChanged -= OnIsEnableChanged;
            }
        }

        protected void RepresentBackground()
        {
            if (this.AssociatedObject.Visibility != Visibility.Visible)
            {
                this.AssociatedObject.Background = this.AssociatedObject.IsChecked == true ? BackgroundCheckedNormal : BackgroundUncheckedNormal;
                this._is_mouse_enter = false;
                this._is_left_mouse_down = false;
                return;
            }
            if (!this.AssociatedObject.IsEnabled) this.AssociatedObject.Background = BackgroundDisabled;
            else
            {
                if (this.AssociatedObject.IsChecked == true)
                {
                    if (_is_left_mouse_down)
                    {
                        this.AssociatedObject.Background = BackgroundCheckedPressed;
                    }
                    else
                    {
                        if (_is_mouse_enter)
                        {
                            this.AssociatedObject.Background = BackgroundCheckedMouseOver;
                        }
                        else
                        {
                            this.AssociatedObject.Background = BackgroundCheckedNormal;
                        }
                    }
                }
                else
                {
                    if (_is_left_mouse_down)
                    {
                        this.AssociatedObject.Background = BackgroundUncheckedPressed;
                    }
                    else
                    {
                        if (_is_mouse_enter)
                        {
                            this.AssociatedObject.Background = BackgroundUncheckedMouseOver;
                        }
                        else
                        {
                            this.AssociatedObject.Background = BackgroundUncheckedNormal;
                        }
                    }

                }
            }
        }

        protected void RepresendBackgroundAsync()
        {
            this.Dispatcher.BeginInvoke(RepresentBackground);
        }

        protected void OnIsEnableChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            RepresendBackgroundAsync();
        }

        protected void OnIsCheckChanged(object sender, RoutedEventArgs e)
        {
            RepresendBackgroundAsync();
        }

        protected void OnMouseEnter(object sender, MouseEventArgs e)
        {
            _is_mouse_enter = true;
            RepresendBackgroundAsync();
        }
        protected void OnMouseLeave(object sender, MouseEventArgs e)
        {
            _is_mouse_enter = false;
            RepresendBackgroundAsync();
        }
        protected void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _is_left_mouse_down = true;
            RepresendBackgroundAsync();
        }
        protected void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _is_left_mouse_down = false;
            RepresendBackgroundAsync();
        }
        bool _is_left_mouse_down = false;
        bool _is_mouse_enter = false;

        #region BackgroundUncheckedNormal (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundUncheckedNormal
        {
            get { return (Brush)GetValue(BackgroundUncheckedNormalProperty); }
            set { SetValue(BackgroundUncheckedNormalProperty, value); }
        }
        public static readonly DependencyProperty BackgroundUncheckedNormalProperty =
            DependencyProperty.Register("BackgroundUncheckedNormal", typeof(Brush), typeof(Background7StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundUncheckedNormalChanged)));

        private static void OnBackgroundUncheckedNormalChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background7StateBehavior source = (Background7StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundUncheckedMouseOver (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundUncheckedMouseOver
        {
            get { return (Brush)GetValue(BackgroundUncheckedMouseOverProperty); }
            set { SetValue(BackgroundUncheckedMouseOverProperty, value); }
        }
        public static readonly DependencyProperty BackgroundUncheckedMouseOverProperty =
            DependencyProperty.Register("BackgroundUncheckedMouseOver", typeof(Brush), typeof(Background7StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundUncheckedMouseOverChanged)));

        private static void OnBackgroundUncheckedMouseOverChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background7StateBehavior source = (Background7StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundUncheckedPressed (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundUncheckedPressed
        {
            get { return (Brush)GetValue(BackgroundUncheckedPressedProperty); }
            set { SetValue(BackgroundUncheckedPressedProperty, value); }
        }
        public static readonly DependencyProperty BackgroundUncheckedPressedProperty =
            DependencyProperty.Register("BackgroundUncheckedPressed", typeof(Brush), typeof(Background7StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundUncheckedPressedChanged)));

        private static void OnBackgroundUncheckedPressedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background7StateBehavior source = (Background7StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundCheckedNormal (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundCheckedNormal
        {
            get { return (Brush)GetValue(BackgroundCheckedNormalProperty); }
            set { SetValue(BackgroundCheckedNormalProperty, value); }
        }
        public static readonly DependencyProperty BackgroundCheckedNormalProperty =
            DependencyProperty.Register("BackgroundCheckedNormal", typeof(Brush), typeof(Background7StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundCheckedNormalChanged)));

        private static void OnBackgroundCheckedNormalChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background7StateBehavior source = (Background7StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundCheckedMouseOver (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundCheckedMouseOver
        {
            get { return (Brush)GetValue(BackgroundCheckedMouseOverProperty); }
            set { SetValue(BackgroundCheckedMouseOverProperty, value); }
        }
        public static readonly DependencyProperty BackgroundCheckedMouseOverProperty =
            DependencyProperty.Register("BackgroundCheckedMouseOver", typeof(Brush), typeof(Background7StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundCheckedMouseOverChanged)));

        private static void OnBackgroundCheckedMouseOverChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background7StateBehavior source = (Background7StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundCheckedPressed (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundCheckedPressed
        {
            get { return (Brush)GetValue(BackgroundCheckedPressedProperty); }
            set { SetValue(BackgroundCheckedPressedProperty, value); }
        }
        public static readonly DependencyProperty BackgroundCheckedPressedProperty =
            DependencyProperty.Register("BackgroundCheckedPressed", typeof(Brush), typeof(Background7StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundCheckedPressedChanged)));

        private static void OnBackgroundCheckedPressedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background7StateBehavior source = (Background7StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion

        #region BackgroundDisabled (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Brush BackgroundDisabled
        {
            get { return (Brush)GetValue(BackgroundDisabledProperty); }
            set { SetValue(BackgroundDisabledProperty, value); }
        }
        public static readonly DependencyProperty BackgroundDisabledProperty =
            DependencyProperty.Register("BackgroundDisabled", typeof(Brush), typeof(Background7StateBehavior),
            new PropertyMetadata(null, new PropertyChangedCallback(OnBackgroundDisabledChanged)));

        private static void OnBackgroundDisabledChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            Background7StateBehavior source = (Background7StateBehavior)sender;

            // 处理逻辑
            Brush newValue = (Brush)args.NewValue;
        }

        #endregion
    }
}
